﻿using System;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Xpedite.Resources;
using Xpedite.Resources.Builders;
using Xpedite.Resources.Loaders;
using Xpedite.Resources.Minifiers;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Test.Resources.Builders
{
  [TestClass]
  public class JavaScriptResourceBuilderTest
  {
    private readonly Mock<IMinifier> _minifier = new Mock<IMinifier>(MockBehavior.Strict);
    private readonly Mock<IResourceUrl> _resourceUrl = new Mock<IResourceUrl>(MockBehavior.Strict);
    private readonly Mock<IResourceLoader> _resourceLoader = new Mock<IResourceLoader>(MockBehavior.Strict);
    private readonly Mock<IMinifierFactory> _minifierFactory = new Mock<IMinifierFactory>(MockBehavior.Strict);
    private readonly Mock<IResourceLoaderFactory> _resourceLoaderFactory = new Mock<IResourceLoaderFactory>(MockBehavior.Strict);

    [TestMethod]
    public void TestBuild()
    {
      var lastModified = DateTime.Now;
      var url = new Uri("http://localhost/resource.ext");

      _resourceLoaderFactory.Setup(mock => mock.Create(_resourceUrl.Object)).Returns(_resourceLoader.Object);
      _resourceLoader.Setup(mock => mock.LoadData()).Returns(new ResourceData { Bytes = Encoding.UTF8.GetBytes("My Content"), Encoding = Encoding.UTF8, LastModified = lastModified });

      _minifierFactory.Setup(mock => mock.Create(ResourceType.JavaScript)).Returns(_minifier.Object);
      _minifier.Setup(mock => mock.Minify("My Content")).Returns("My Minified Content");

      var container = new JavaScriptResourceBuilder(_resourceUrl.Object, _minifierFactory.Object, _resourceLoaderFactory.Object).Build();
      var resource = container.Resource;

      Assert.AreSame(_resourceUrl.Object, container.ResourceUrl);
      Assert.AreEqual(ResourceType.JavaScript, container.ResourceType);

      Assert.IsTrue(resource.Compressable);
      Assert.AreEqual(lastModified, resource.LastModified);
      Assert.AreEqual(ResourceType.JavaScript, resource.Type);
      Assert.AreEqual(Encoding.UTF8.WebName, resource.Encoding.WebName);
      Assert.AreEqual("My Minified Content", Encoding.UTF8.GetString(resource.Bytes));
    }
  }
}
